package com.sky.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryExtensionsKt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //封装ShoppingCart类
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //封装userId
        shoppingCart.setUserId(userId);

        //查看是否添加过,并返回
        ShoppingCart newShoppingCart = shoppingCartMapper.findShoppingCartById(shoppingCart);
        //判断是否为第一次添加
        if (newShoppingCart==null) {
            //判断是菜品还是套餐
            if (shoppingCart.getDishId()==null) {
                //通过套餐id获取套餐属性并封装
                Setmeal setmeal = setmealMapper.setmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else {
                //通过菜品id获取菜品属性并封装
                Dish dish = dishMapper.dishById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //共有属性
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加
            shoppingCartMapper.add(shoppingCart);
        }else{
            //增加number
            newShoppingCart.setNumber(newShoppingCart.getNumber()+1);
            //修改
            shoppingCartMapper.updateNumberById(newShoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCart = shoppingCartMapper.getCurrentId(userId);

        return shoppingCart;
    }

    /**
     * 删除判断
     * @param shoppingCartDTO
     */
    @Override
    public void delShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        //返回user对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);
        ShoppingCart newShoppingCart = shoppingCartMapper.findShoppingCartById(shoppingCart);
        if (newShoppingCart.getNumber()>1) {
            newShoppingCart.setNumber(newShoppingCart.getNumber()-1);
            shoppingCartMapper.updateNumberById(newShoppingCart);
        }else {
            shoppingCartMapper.delShoppingCart(shoppingCart);
        }
    }

    @Override
    public void deleteCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteCart(userId);
    }
}
